package com.thc.controller;

import com.thc.domin.Order;
import com.thc.domin.OrderAAA;
import com.thc.domin.Ticket;
import com.thc.domin.User;
import com.thc.feignclient.MovieFeignClient;
import com.thc.feignclient.UserFeignClient;
import com.thc.service.OrderService;
import com.thc.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Random;

//订单服务
@RestController
@RefreshScope  //刷新配置
public class OrderController {

    //@Value("${test.name}")
    //private String name;
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private MovieFeignClient movieFeignClient;

    @GetMapping("order")
    public R<Void> findOrder(@RequestBody OrderAAA order){
        User user = userFeignClient.findById(order.getUserId());
        Ticket ticket = movieFeignClient.findById(order.getMovieId());
        int ticketNum = new Random().nextInt(1000) + 100312414;
        //计算存入totalprice
        //BigDecimal price = ticket.getPrice();
        //Long num=order.getNum();
        //BigDecimal result=price.multiply(new BigDecimal(num));
        //判断数据库中是否有这本书
        if(user==null||ticket==null){
            return R.fail("购买失败,没有这本书");
        }
        Order aaaa = Order.builder()
                .ordersn(BigDecimal.valueOf(ticketNum))
                .username(user.getUsername())
                .moviename(ticket.getMovieName())
                .num(order.getNum())
                .totalprice(ticket.getPrice().multiply(new BigDecimal(order.getNum())))
                .build();
        orderService.add(aaaa);
        return R.ok("购买成功");
    }
}
